﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using Infragistics.Windows.DataPresenter;

namespace PTExpress.Templates
{
    /// <summary>
    /// Interaction logic for ucDmTemplate.xaml
    /// </summary>
    /// 
    public enum Command { None, View, Edit, New, Copy };
    public partial class ucDmTemplate : UserControl
    {
        bool first = true;
        public bool setcolor = false;
        public DataTable CurentRow = new DataTable();
        string[] columns;
        string[] values;
        public ucDmTemplate()
        {
            InitializeComponent();
            
        }

        public ucDmTemplate(string image, string title, DataTable datasource, string stringbrowse, object footer)
        {
            InitializeComponent();
            
            this.Image = image;
            this.TableSource = datasource;
            this.Title = title;
            this.StringBrowse = stringbrowse;
            this.Footer = footer;
        }

        public void Update(DataTable tb)
        {
            this.TableSource = tb;
            xdgResult.DataSource = tb.DefaultView;
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(ucDmTemplate), new UIPropertyMetadata());

        

        public string Image
        {
            get { return (string)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Image.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(string), typeof(ucDmTemplate), new UIPropertyMetadata());



        public bool btnThoat_Visibility
        {
            get { return (bool)GetValue(btnThoat_VisibilityProperty); }
            set { SetValue(btnThoat_VisibilityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for btnThoat_Visibility.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty btnThoat_VisibilityProperty =
            DependencyProperty.Register("btnThoat_Visibility", typeof(bool), typeof(ucDmTemplate), new UIPropertyMetadata(false));



        public double HeaderHeigth
        {
            get { return (double)GetValue(HeaderHeigthProperty); }
            set { SetValue(HeaderHeigthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HeaderHeigth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderHeigthProperty =
            DependencyProperty.Register("HeaderHeigth", typeof(double), typeof(ucDmTemplate), new UIPropertyMetadata(null));


        public DataTable TableSource = new DataTable();
        public string StringBrowse = string.Empty;

        public object Footer
        {
            get { return (object)GetValue(FooterProperty); }
            set { SetValue(FooterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Footer.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FooterProperty =
            DependencyProperty.Register("Footer", typeof(object), typeof(ucDmTemplate), new UIPropertyMetadata(0));

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!first)
                return;
            this.AddHandler(Button.ClickEvent, new RoutedEventHandler(ToolBarButton_Click));
            Templates.DataGrid.FieldSetting(ref xdgResult, StringBrowse);
            
            xdgResult.DataSource = this.TableSource.DefaultView;
            AddSummary();
            if(setcolor)
                SetRowColorByTag(ColumnTag, ArrValue, System.Windows.Media.Color.FromRgb(0, 128, 255));
            first = false;
        }



        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(ucDmTemplate), new UIPropertyMetadata(false));

            
        //Ẩn hiện button
        private void ToolBar_Loaded(object sender, RoutedEventArgs e)
        {
            if (F2 == null)
                (this.FindName("btnXem") as Button).Visibility = Visibility.Collapsed;
            if (F4 == null)
                (this.FindName("btnThemMoi") as Button).Visibility = Visibility.Collapsed;
            if (F3 == null)
                (this.FindName("btnSua") as Button).Visibility = Visibility.Collapsed;
            if (F8 == null)
                (this.FindName("btnXoa") as Button).Visibility = Visibility.Collapsed;
            if (F6 == null)
                (this.FindName("btnRefresh") as Button).Visibility = Visibility.Collapsed;
            if (F7 == null)
                (this.FindName("btnSaoChep") as Button).Visibility = Visibility.Collapsed;
            if (F5 == null)
                (this.FindName("btnChiTiet") as Button).Visibility = Visibility.Collapsed;
            if (F9 == null)
                (this.FindName("btnReport") as Button).Visibility = Visibility.Collapsed;
        }

        //Button Click
        private void ToolBarButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Button btn = (e.OriginalSource as Button);
            if (btn == null)
                return;
            try
            {
                switch (btn.Name)
                {
                    case "btnThemMoi":
                        {
                            EventArgs MyArgs = new EventArgs();
                            F4(this, MyArgs);

                        }
                        break;
                    case "btnSua":
                        {
                            EventArgs MyArgs = new EventArgs();
                            F3(this, MyArgs);
                        }
                        break;
                    case "btnXoa":
                        {
                            EventArgs MyArgs = new EventArgs();
                            F8(this, MyArgs);
                        }
                        break;
                    case "btnSaoChep":
                        {
                            EventArgs MyArgs = new EventArgs();
                            F7(this, MyArgs);
                        }
                        break;
                    case "btnXem":
                        {
                            EventArgs MyArgs = new EventArgs();
                            F2(this, MyArgs);
                        }
                        break;
                    case "btnRefresh":
                        {
                            EventArgs MyArgs = new EventArgs();
                            F6(this, MyArgs);
                        }
                        break;
                    case "btnChiTiet":
                        {
                            EventArgs MyArgs = new EventArgs();
                            F5(this, MyArgs);
                        }
                        break;
                    case "btnReport":
                        {
                            EventArgs MyArgs = new EventArgs();
                            F9(this, MyArgs);
                        }
                        break;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"Lỗi",MessageBoxButton.OK,MessageBoxImage.Error);
            }
        }

        public delegate void GridKeyUp_F2(object sender, EventArgs e);
        public delegate void GridKeyUp_F3(object sender, EventArgs e);
        public delegate void GridKeyUp_F4(object sender, EventArgs e);
        public delegate void GridKeyUp_F5(object sender, EventArgs e);
        public delegate void GridKeyUp_F6(object sender, EventArgs e);
        public delegate void GridKeyUp_F7(object sender, EventArgs e);
        public delegate void GridKeyUp_F8(object sender, EventArgs e);
        public delegate void GridKeyUp_F9(object sender, EventArgs e);
        public delegate void GridKeyUp_F10(object sender, EventArgs e);
        public delegate void GridKeyUp_F11(object sender, EventArgs e);
        public delegate void GridKeyUp_F12(object sender, EventArgs e);

        public event GridKeyUp_F2 F2;
        public event GridKeyUp_F3 F3;
        public event GridKeyUp_F4 F4;
        public event GridKeyUp_F5 F5;
        public event GridKeyUp_F6 F6;
        public event GridKeyUp_F7 F7;
        public event GridKeyUp_F8 F8;
        public event GridKeyUp_F9 F9;
        public event GridKeyUp_F10 F10;
        public event GridKeyUp_F11 F11;
        public event GridKeyUp_F12 F12;

        //#region uc_KeyUp

        //Preview KeyDown
        void uc_PreviewKeyDown(object sender, KeyEventArgs e)
        {

            switch (e.Key)
            {
                #region Khai bao tung key forward

                case Key.F2:
                    if (F2 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F2(this, MyArgs);
                    }
                    break;
                case Key.F3:
                    if (F3 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F3(this, MyArgs);
                    }
                    break;
                case Key.F4:
                    if (F4 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F4(this, MyArgs);
                    }
                    break;
                case Key.F5:
                    if (F5 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F5(this, MyArgs);
                    }
                    break;
                case Key.F6:
                    if (F6 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F6(this, MyArgs);
                    }
                    break;
                case Key.F7:
                    if (F7 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F7(this, MyArgs);
                    }
                    break;
                case Key.F8:
                    if (F8 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F8(this, MyArgs);
                    }
                    break;
                case Key.F9:
                    if (F9 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F9(this, MyArgs);
                    }
                    break;
                case Key.F10:
                    if (F10 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F10(this, MyArgs);
                    }
                    break;
                case Key.F11:
                    if (F11 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F11(this, MyArgs);
                    }
                    break;
                case Key.F12:
                    if (F12 != null)
                    {
                        EventArgs MyArgs = new EventArgs();
                        F12(this, MyArgs);
                    }
                    break;
                default:
                    break;
                #endregion
            }
        }

        //Get Columns
        private string GetColumns(DataRecord record)
        {
            string cl = string.Empty;
            foreach (Cell c in record.Cells)
                cl += c.Field.Name + ",";
            if (cl.Length > 0)
                cl = cl.Substring(0, cl.Length);
            return cl;
        }

        private void xdgResult_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                DataRecord record = xdgResult.ActiveRecord as DataRecord;
                CurentRow = TableSource.Select(record.Cells[0].Field.Name + " = '" + record.Cells[0].Value.ToString() + "'").CopyToDataTable();
                EventArgs MyArgs = new EventArgs();
                F2(this, MyArgs);
            }
            catch
            {
                CurentRow = null;
            }
        }

        private void xdgResult_CellActivated(object sender, Infragistics.Windows.DataPresenter.Events.CellActivatedEventArgs e)
        {
            if (xdgResult.ActiveCell == null)
                return;
            try
            {
                DataRecord record = xdgResult.ActiveRecord as DataRecord;
                CurentRow = TableSource.Select(record.Cells[0].Field.Name + " = '" + record.Cells[0].Value.ToString() + "'").CopyToDataTable();
                IsSelected = true;
            }
            catch
            { }
        }

        //public Add summary
        public void AddSummary()
        {
            if (columns == null || values == null)
                return;
            if (xdgResult.FieldLayouts[0].SummaryDefinitions.Count > 0)
                xdgResult.FieldLayouts[0].SummaryDefinitions.Clear();

            for (int i = 0; i < columns.Count(); i++)
            {
                SummaryDefinition sm = new SummaryDefinition();
                sm.SourceFieldName = columns[i];
                sm.StringFormat = values[i];
                sm.Calculator = SummaryCalculator.Count; 
                xdgResult.FieldLayouts[0].SummaryDefinitions.Add(sm);
            }
        }

        //Add summary
        public void AddSummaryValue(string[] columns,string[] values)
        {
            this.columns = columns;
            this.values = values;
            
        }

        #region 'SETCOLOR DATAROW'
        //SetColor
        System.Windows.Style defaultDataRecordCellAreaStyle;
        string ColumnTag = string.Empty;
        string ArrValue = string.Empty;

        //Setcolor Ftag
        public void SetRowColorByTag(String ColumnTag, String ArrValue)
        {
            setcolor = true;
            this.ColumnTag = ColumnTag;
            this.ArrValue = ArrValue;
        }

        public void SetRowColorByTag(String ColumnTag, String ArrValue, Color color)
        {
            defaultDataRecordCellAreaStyle = xdgResult.FieldLayoutSettings.DataRecordCellAreaStyle;
            System.Windows.Style style = new System.Windows.Style(typeof(DataRecordCellArea));
            if (defaultDataRecordCellAreaStyle == null)
            {
                style.BasedOn = Infragistics.Windows.Themes.DataPresenterOffice2k7Blue.DataRecordCellArea;
            }
            else
            {
                style.BasedOn = defaultDataRecordCellAreaStyle;
            }
            System.Windows.Data.Binding bd = new System.Windows.Data.Binding("Record.DataItem." + ColumnTag);
            bd.RelativeSource = new System.Windows.Data.RelativeSource(System.Windows.Data.RelativeSourceMode.Self);
            System.Windows.Setter setter = new System.Windows.Setter(DataRecordCellArea.ForegroundProperty, new System.Windows.Media.SolidColorBrush(color));
            String[] Values = ArrValue.Split(';');
            System.Windows.DataTrigger[] DataTriggers = new System.Windows.DataTrigger[Values.Length];

            for (int i = 0; i < Values.Length; i++)
            {
                DataTriggers[i] = new DataTrigger();
                DataTriggers[i].Binding = bd;
                DataTriggers[i].Value = Values[i];
                DataTriggers[i].Setters.Add(setter);
                style.Triggers.Add(DataTriggers[i]);
            }

            try
            {
                //if (frmBrw.oBrowse.FieldLayoutSettings.DataRecordCellAreaStyle == null)
                xdgResult.FieldLayoutSettings.DataRecordCellAreaStyle = style;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        private void xdgResult_Unloaded(object sender, RoutedEventArgs e)
        {
        }

    }
}
